﻿using iMES.Core.Extensions;
using iMES.Core.Utilities;
using iMES.Entity.DomainModels;
using iMES.Mass.Repositories;
using iMES.Mass.IRepositories;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace iMES.Mass.Services
{
    public partial class Mass_DefectsService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IMass_DefectsRepository Mass_Defects_repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Mass_DefectsService(
            IMass_DefectsRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            Mass_Defects_repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            return base.Add(saveDataModel);
        }
        /// <summary>
        /// 编辑操作
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveModel)
        {
            UpdateOnExecute = (SaveModel model) =>
            {
                return webResponse.OK();
            };
            //编辑方法保存数据库前处理
            UpdateOnExecuting = (Mass_Defects model, object addList, object updateList, List<object> delKeys) =>
            {
                return webResponse.OK();
            };
            return base.Update(saveModel);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="delList"></param>
        /// <returns></returns>
        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            for (int i = 0; i < keys.Length; i++)
            {
                List<object> idsProject = Mass_DefectsRepository.Instance.FindAsIQueryable
                    (x => x.Mass_Defects_ID == keys[i].GetInt()).Select(s => (object)s.Mass_Defects_ID).ToList();
                object[] arrayProject = idsProject.ToArray();
                Mass_Defects_repository.DeleteWithKeys(arrayProject, true);
            }
            //最终保存
            repository.SaveChanges();
            return base.Del(keys, true);
        }
    }
}
